const app = getApp();
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    logo: '/static/settings/logo.png',
    searchForm: {
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
    },
    totalPages: 0,
    loading: false, //是否正在加载数据
    list: [],
  },

  //当前页发生改变
  changePage(v) {
    this.setData({
      'searchForm.pageNumber': v
    });
    this.getExhibitorData();
  },
  //查询方法
  handleSearch() {
    this.setData({
      'searchForm.pageNumber': 1
    });
    this.getExhibitorData();
  },
  //获取活动数据
  getExhibitorData: function() {


    let that = this;
    that.setData({
      loading: true
    })
    //加载信息
    util.request(api.EXHIBITOR_COLLECT_LIST, that.data.searchForm).then(function(res) {
    
      if (res.success === true) {
        let data = new Object();
        let list = [];
        if (that.data.searchForm.pageNumber > 1) { //加载更多  追加信息
          list = that.data.list;
        }
        list.push(...res.result.records)
        data.list = list;
        data.totalPages = res.result.pages;
        data.loading = false;
        that.setData(data);
      }
    });
  },

  //活动详情
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/exhibitor/exhibitor-detail/index?id=' + e.currentTarget.dataset.id,
    });
  },
  //取消收藏
  collectCancel: function(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let that = this;
    let list = that.data.list;
    list.splice(index, 1)
    util.request(api.EXHIBITOR_COLLECT_CANCEL + '/' + id).then(function(res) {

    });
    that.setData({
      'list': list
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let list = this.data.list
    if (!list || list.length == 0) {
      this.handleSearch();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.changePage(++this.data.searchForm.pageNumber);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})